﻿using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using PlanningGIS.Framework;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.File
{
    /// <summary>
    /// 作用：新建工作区
    /// 作者：
    /// 编写日期:2007-08-01
    /// </summary>
    public class NewProject : BaseCommand
    {
        /// <summary>
        /// 
        /// </summary>
        IApplication m_Application = null;
        /// <summary>
        /// 
        /// </summary>
        public NewProject()
        {
            base.m_caption = "新建工作区";
            base.m_name = "NEW";
            base.m_toolTip = "新建工作区";
            base.m_message = "新建工作区";
            base.m_category = "新建工作区";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="hook"></param>
        public override void OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public override void OnClick()
        {
            try
            {
                IMxDocument mxDocment = (m_Application as ApplicationClass).MxDocument;
                if (mxDocment != null)
                {
                    IMapDocument mapDocment = (mxDocment as MxDocumentClass).MapDocument;
                    if (mapDocment != null)
                    {
                        DialogResult dr = DialogResult.Cancel;
                        try
                        {
                            if (mapDocment.DocumentFilename == "")
                            {
                                dr = MsgHelper.ShowYesNoCancelQueryMsgbox("是否需要保存当前模板？");
                                if (dr == DialogResult.Yes)
                                {
                                    SaveAsMxd saveAsMxd = new SaveAsMxd();
                                    saveAsMxd.OnCreate(m_Application);
                                    saveAsMxd.OnClick();

                                }
                                else if (dr == DialogResult.Cancel) return;
                            }
                            else
                            {
                                dr = MsgHelper.ShowYesNoCancelQueryMsgbox("是否需要保存‘" + mapDocment.DocumentFilename + "’？");
                                if (dr == DialogResult.Yes)
                                {
                                    m_Application.SaveDocument("");
                                }
                                else if (dr == DialogResult.Cancel) return;
                            }
                        }
                        catch
                        {
                            dr = MsgHelper.ShowYesNoCancelQueryMsgbox("是否需要保存当前模板？");
                            if (dr == DialogResult.Yes)
                            {
                                SaveAsMxd saveAsMxd = new SaveAsMxd();
                                saveAsMxd.OnCreate(m_Application);
                                saveAsMxd.OnClick();

                            }
                            else if (dr == DialogResult.Cancel) return;
                        }

                    }
                }

                m_Application.NewDocument(false, "");
                ((m_Application.Document as IMxDocument).ActiveView as IGraphicsContainer).DeleteAllElements();
                (m_Application.Document as IMxDocument).ActiveView.Refresh();
                (m_Application as IFramework).TOCControl.Update();

                if (AppConfig.SystemConfig.鹰眼图.鹰眼类型 == "0")
                {
                    Control control = (m_Application as IFramework).DockManager.GetControlFromDockPanel("鹰眼图");
                    if (control == null) return;
                    PlanningGIS.Framework.Controls.OverViewPanel overViewPanel = control.Controls[0] as PlanningGIS.Framework.Controls.OverViewPanel;
                    if (overViewPanel == null) return;
                    overViewPanel.pEvent_OnMapReplaced(null);
                }
            }
            catch
            {
            }
        }

        /// <summary>
        /// 图标
        /// </summary>
        public override int Bitmap
        {
            get
            {
                string resourceName = this.GetType().Namespace + ".Res." + this.GetType().Name + ".png";
                return new Bitmap(GetType().Assembly.GetManifestResourceStream(resourceName)).GetHbitmap().ToInt32();
            }
        }

        /// <summary>
        /// 可用状态
        /// </summary>
        public override bool Enabled
        {
            get
            {
                if (m_Application == null) return false; 
                return (m_Application as IFramework).IsMapCtrlactive;
            }
        }
    }
}